package com.umerclass.ui.home;

import android.content.Intent;
import android.net.Uri;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.Toast;
import androidx.recyclerview.widget.GridLayoutManager;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;
import com.bumptech.glide.Glide;
import com.bumptech.glide.RequestManager;
import com.facebook.drawee.view.SimpleDraweeView;
import com.stx.xhb.xbanner.XBanner;
import com.umerclass.AppDroid;
import com.umerclass.R;
import com.umerclass.bean.anmo.UserOtherBean;
import com.umerclass.bean.home.BannerBean;
import com.umerclass.bean.home.DataBean;
import com.umerclass.bean.home.ItemOneBean;
import com.umerclass.bean.home.VideoBean;
import com.umerclass.frame.model.InfoResult;
import com.umerclass.frame.ui.fragment.BaseFragment;
import com.umerclass.frame.ui.recycleview.listener.OptListener;
import com.umerclass.ui.anmo.AnMoLoginActivity;
import com.umerclass.ui.anmo.DaiLiActivity;
import com.umerclass.ui.anmo.customer.CustomerHomeActivity;
import com.umerclass.ui.home.adapter.ImageBannerAdapter;
import com.umerclass.ui.home.adapter.ItemAdapterOne;
import com.umerclass.ui.home.adapter.VideoAdapterTwo;
import com.youth.banner.Banner;
import com.youth.banner.config.IndicatorConfig;
import com.youth.banner.indicator.CircleIndicator;
import com.youth.banner.util.BannerUtils;
import java.util.ArrayList;
import java.util.List;
import butterknife.BindView;
import butterknife.OnClick;

public class HomeFragment extends BaseFragment implements OptListener {

    @BindView(R.id.linear_search)
    LinearLayout linearSearch;
    @BindView(R.id.banner)
    Banner banner;
    @BindView(R.id.recycleview)
    RecyclerView recycleview;
    @BindView(R.id.xbanner)
    XBanner xbanner;
    @BindView(R.id.linear_more)
    LinearLayout linearMore;
    @BindView(R.id.recycleview2)
    RecyclerView recycleview2;
    @BindView(R.id.linear1)
    LinearLayout linear1;
    @BindView(R.id.linear2)
    LinearLayout linear2;
    @BindView(R.id.linear3)
    LinearLayout linear3;
    @BindView(R.id.linear4)
    LinearLayout linear4;
    @BindView(R.id.linear5)
    LinearLayout linear5;
    @BindView(R.id.view)
    View view;
    private ItemAdapterOne itemAdapterOne;
    private RequestManager glideRequestManager;
    private GridLayoutManager gridLayoutManager;
    private List<String> paths = new ArrayList<>();
    private VideoAdapterTwo videoAdapterTwo;
    private List<VideoBean> dataList = new ArrayList<>();
    private LinearLayoutManager linearLayoutManager;

    @Override
    public boolean isImmersionBarEnabled() {
        return false;
    }

    @Override
    public int getLayoutResId() {
        return R.layout.fragment_home;
    }

    @Override
    public void init() {
        settStateBar();
        glideRequestManager = Glide.with(this);
        gridLayoutManager = new GridLayoutManager(getActivity(), 5);
        recycleview.setLayoutManager(gridLayoutManager);
       /* LinearLayout.LayoutParams layoutParams = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT,
                ScreenUtil.getInstance().getScreenWidth() / 2);
        xbanner.setLayoutParams(layoutParams);*/
        initBanner(xbanner);

        paths.add("https://timgsa.baidu.com/timg?image&quality=80&size=b9999_10000&sec=1584700877125&di=60533ae9f1a3696ac9fce5da982d4e80&imgtype=0&src=http%3A%2F%2Fpic.feizl.com%2Fupload%2Fallimg%2F170909%2F090051hmf5ju5oy.jpg");
        paths.add("https://timgsa.baidu.com/timg?image&quality=80&size=b9999_10000&sec=1584700877121&di=631f8f3ac8106ba0e06697302d1efc7f&imgtype=0&src=http%3A%2F%2Fpic1.win4000.com%2Fpic%2Fc%2F00%2F263e1506139.jpg");
        paths.add("https://timgsa.baidu.com/timg?image&quality=80&size=b9999_10000&sec=1584700877116&di=c2ce8d420822602996da52d4bdeb71a8&imgtype=0&src=http%3A%2F%2Fpic.feizl.com%2Fupload%2Fallimg%2F170903%2F0323sxux4gf3jok.jpg");
        setBannerDatas(paths);

        todo();
        todoListVideo();

        linearLayoutManager = new LinearLayoutManager(getActivity());
        videoAdapterTwo = new VideoAdapterTwo(this, dataList);
        recycleview2.setAdapter(videoAdapterTwo);
        recycleview2.setLayoutManager(linearLayoutManager);
    }

    @Override
    public void loadData() {

    }

    @OnClick({R.id.linear_more, R.id.linear1, R.id.linear2, R.id.linear3, R.id.linear4, R.id.linear5})
    public void OnClick(View view) {
        switch (view.getId()) {
            //查看更多内容
            case R.id.linear_more:
                break;
            //活动专栏
            case R.id.linear1:
                break;
            //免费
            case R.id.linear2:
                break;
            //试听
            case R.id.linear3:
                break;
            //会员
            case R.id.linear4:
                break;
            //联盟舰旗店
            case R.id.linear5:
                break;
            default:
                break;
        }
    }

    @Override
    public void onOptClick(View view, Object object) {
        switch (view.getId()) {
            //头部
            case R.id.iv_bofang:
                //尾部
            case R.id.iv_bofang2:
                VideoBean videoBean = (VideoBean) object;
                int id = videoBean.getId();
                String path = videoBean.getPath();
               /* Intent intent = new Intent(getActivity(), VideoDetailsActivity.class);
                intent.putExtra("id", id);
                intent.putExtra("path", path);
                startActivity(intent);*/
               switchTo(getActivity(),VoiceActivity.class,false);
                break;
            case R.id.linear:
                int index = (int) object;
                if (index == 0) {
                    //switchTo(getActivity(), JoinActivity.class,false);
                    //switchTo(getActivity(), AnMoLoginActivity.class,false);SimpleDraweeView
                    switchTo(getActivity(), CustomerHomeActivity.class, false);
                } else if (index == 1) {
                    switchTo(getActivity(), VoiceActivity.class, false);
                } else if (index == 2) {
                    switchTo(getActivity(), VideoActivity.class, false);
                } else if (index == 3) {
                    switchTo(getActivity(), FileActivity.class, false);
                } else if (index == 4) {
                    //switchTo(getActivity(), JoinActivity.class,false);
                    if (AppDroid.getInstance().getUserOtherBean() != null) {
                        UserOtherBean userOtherBean = AppDroid.getInstance().getUserOtherBean();
                        int businessType = userOtherBean.getBusinessType();
                        Intent intent2 = new Intent(getActivity(), DaiLiActivity.class);
                        intent2.putExtra("businessType", businessType);
                        startActivity(intent2);
                    } else {
                        switchTo(getActivity(), AnMoLoginActivity.class, false);
                    }
                }
                break;
            default:
                break;
        }
    }

    @Override
    protected void onSuccess(int what, InfoResult t) {

    }

    @Override
    protected void onFail(int what, InfoResult t) {

    }

    //todo
    private void todo() {
        List<DataBean> infos = new ArrayList<>();
        List<ItemOneBean> infos2 = new ArrayList<>();
        DataBean dataBean = new DataBean();
        dataBean.setPath("https://timgsa.baidu.com/timg?image&quality=80&size=b9999_10000&sec=1583400903763&di=8a76541ee45e0fd34b8376ad736dd78e&imgtype=0&src=http%3A%2F%2Fphotocdn.sohu.com%2F20141218%2FImg407062632.jpg");
        DataBean dataBean2 = new DataBean();
        dataBean2.setPath("https://timgsa.baidu.com/timg?image&quality=80&size=b9999_10000&sec=1583401003849&di=cf08434fa3ac40b79f982ec556f4ad4e&imgtype=0&src=http%3A%2F%2Fs9.rr.itc.cn%2Fg%2FwapChange%2F20147_3_16%2Fa3sf924048559674323.jpg");
        DataBean dataBean3 = new DataBean();
        dataBean3.setPath("https://timgsa.baidu.com/timg?image&quality=80&size=b9999_10000&sec=1583401026382&di=f9e1c298e8ca6543167d0e6911ba1207&imgtype=0&src=http%3A%2F%2Fs13.sinaimg.cn%2Fmw690%2F002snjTagy6LetknaNK1c%26690");
        infos.add(dataBean);
        infos.add(dataBean2);
        infos.add(dataBean3);
        ImageBannerAdapter imageBannerAdapter = new ImageBannerAdapter(infos, glideRequestManager);
        banner.setAdapter(imageBannerAdapter);
        banner.setIndicator(new CircleIndicator(getActivity()));
        banner.setIndicatorNormalColor(getResources().getColor(R.color.white));
        banner.setIndicatorSelectedColor(getResources().getColor(R.color.green));
        banner.setIndicatorGravity(IndicatorConfig.Direction.LEFT);
        banner.setIndicatorSpace(BannerUtils.dp2px(20));
        banner.setIndicatorMargins(new IndicatorConfig.Margins((int) BannerUtils.dp2px(10)));
        banner.setIndicatorWidth(10, 20);
        banner.setActivated(true);
        banner.start();


        ItemOneBean itemOneBean = new ItemOneBean();
        itemOneBean.setName("活动报名");
        ItemOneBean itemOneBean2 = new ItemOneBean();
        itemOneBean2.setName("音频");
        ItemOneBean itemOneBean3 = new ItemOneBean();
        itemOneBean3.setName("视频");
        ItemOneBean itemOneBean4 = new ItemOneBean();
        itemOneBean4.setName("文件");
        ItemOneBean itemOneBean5 = new ItemOneBean();
        itemOneBean5.setName("华钛");
        infos2.add(itemOneBean);
        infos2.add(itemOneBean2);
        infos2.add(itemOneBean3);
        infos2.add(itemOneBean4);
        infos2.add(itemOneBean5);
        itemAdapterOne = new ItemAdapterOne(getActivity(), infos2, R.layout.item_home_one, glideRequestManager, this);
        recycleview.setAdapter(itemAdapterOne);
    }

    /**
     * 初始化XBanner
     */
    private void initBanner(XBanner banner) {
        //设置广告图片点击事件
        banner.setOnItemClickListener(new XBanner.OnItemClickListener() {
            @Override
            public void onItemClick(XBanner banner, Object model, View view, int position) {
                Toast.makeText(getActivity(), "点击了第" + (position + 1) + "图片", Toast.LENGTH_SHORT).show();
            }
        });
        //加载广告图片
        banner.loadImage(new XBanner.XBannerAdapter() {
            @Override
            public void loadBanner(XBanner banner, Object model, View view, int position) {
                //此处适用Fresco加载图片，可自行替换自己的图片加载框架
                SimpleDraweeView draweeView = (SimpleDraweeView) view;
                BannerBean listBean = ((BannerBean) model);
                String url = listBean.getPath();
                draweeView.setImageURI(Uri.parse(url));
//                加载本地图片展示
//                ((ImageView)view).setImageResource(((LocalImageInfo) model).getXBannerUrl());
            }
        });
    }

    private void setBannerDatas(List<String> infos) {

        List<BannerBean> bannerBeans = new ArrayList<>();
        for (int i = 0; i < infos.size(); i++) {
            bannerBeans.add(new BannerBean(infos.get(i)));
        }
        //刷新数据之后，需要重新设置是否支持自动轮播
        xbanner.setAutoPlayAble(bannerBeans.size() > 1);
        xbanner.setIsClipChildrenMode(true);
        xbanner.setBannerData(R.layout.layout_fresco_imageview, bannerBeans);
    }

    private void todoListVideo() {
        VideoBean videoBean = new VideoBean();
        videoBean.setPath("https://timgsa.baidu.com/timg?image&quality=80&size=b9999_10000&sec=1584953595523&di=b9d3d55694de888d0773b2b0c4748ada&imgtype=0&src=http%3A%2F%2Fwx4.sinaimg.cn%2Fmw690%2F0071IbvBly1ftfk06y6wuj30k00uyaeb.jpg");
        videoBean.setTxt("jack我爱你");
        videoBean.setPeople(6);

        VideoBean videoBean2 = new VideoBean();
        videoBean2.setPath("https://timgsa.baidu.com/timg?image&quality=80&size=b9999_10000&sec=1584953595523&di=b9d3d55694de888d0773b2b0c4748ada&imgtype=0&src=http%3A%2F%2Fwx4.sinaimg.cn%2Fmw690%2F0071IbvBly1ftfk06y6wuj30k00uyaeb.jpg");
        videoBean2.setTxt("jack我爱你");
        videoBean2.setPeople(6);

        VideoBean videoBean3 = new VideoBean();
        videoBean3.setPath("https://timgsa.baidu.com/timg?image&quality=80&size=b9999_10000&sec=1584953595523&di=b9d3d55694de888d0773b2b0c4748ada&imgtype=0&src=http%3A%2F%2Fwx4.sinaimg.cn%2Fmw690%2F0071IbvBly1ftfk06y6wuj30k00uyaeb.jpg");
        videoBean3.setTxt("jack我爱你");
        videoBean3.setPeople(6);

        dataList.add(videoBean);
        dataList.add(videoBean2);
        dataList.add(videoBean3);
    }

    private void settStateBar() {
        int result = 0;
        int resourceId = this.getResources().getIdentifier("status_bar_height", "dimen", "android");
        if (resourceId > 0) {
            result = this.getResources().getDimensionPixelSize(resourceId);
            showToast("gaodu" + result);
            LinearLayout.LayoutParams linearParams = (LinearLayout.LayoutParams) view.getLayoutParams();
            linearParams.height = result;
            view.setLayoutParams(linearParams);
        } else {

        }
    }
}
